{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 1., 0., 1., 0., 1., 1.],\n",
       "        [0., 0., 1., 0., 1., 0., 1., 1.],\n",
       "        [0., 0., 1., 0., 1., 0., 1., 1.],\n",
       "        [0., 0., 1., 0., 1., 0., 1., 1.]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "A = torch.zeros(4,8)\n",
    "B = torch.ones(4,4)\n",
    "idx = [2,4,6,7]\n",
    "A[:,idx] = B\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "target_name = \"'model.layers.0.mlp.up_proj\"\n",
    "match = re.match(r\".*\\.[^.]*\\.(\\d+)\\.\", target_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "int(match.group(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\".\".join(target_name.split(\".\")[-2:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([], size=(0, 2)), tensor([], size=(0, 0)), tensor([], size=(2, 0)))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.empty((0, 2)), torch.empty((0, 0)), torch.empty((2, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c', 'a', 'e', 'd']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d1 = {\n",
    "    \"a\": 1,\n",
    "    \"b\": 2,\n",
    "    \"c\": 3\n",
    "}\n",
    "\n",
    "d2 = {\n",
    "    \"a\": 10,\n",
    "    \"e\": 20,\n",
    "    \"d\": 40\n",
    "}\n",
    "from itertools import chain\n",
    "list(chain(d1.keys(), d2.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ke2torch23cu121",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
